package com.situ.mall.order.controller;


import com.situ.mall.base.entity.MemberAddress;
import com.situ.mall.base.service.IMemberAddressService;
import com.situ.mall.base.util.OrderSessionUtil;
import com.situ.mall.base.util.URLUtil;
import com.situ.mall.base.service.ICartService;
import com.situ.mall.base.vo.OrderVO;
import com.situ.mall.base.vo.ShopVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author darling
 * @since 2023-04-02
 */
@Controller
@RequestMapping(URLUtil.CART)
public class CartController {

    @Autowired
    private ICartService cartService;

    @Autowired
    private IMemberAddressService memberAddressService;

    @GetMapping(URLUtil.INDEX)
    public String index(){
        return URLUtil.CART_HTML;
    }

    @GetMapping(URLUtil.LIST)
    @ResponseBody
    public ShopVO list(){
        return ShopVO.data(cartService.doList());
    }

    @PostMapping(URLUtil.ADD + URLUtil.UID)
    @ResponseBody
    public void add(@PathVariable Integer id){
        cartService.add(id);
    }

    @PostMapping(URLUtil.SUB + URLUtil.UID)
    @ResponseBody
    public void sub(@PathVariable Integer id){
        cartService.sub(id);
    }


    @PostMapping(URLUtil.DELETE)
    @ResponseBody
    public void delete(Integer cartId){
        cartService.doDel(cartId);
    }

    @PostMapping(URLUtil.CONFIRM)
    public String confirm(String ids, Model model){

        List<OrderVO> list = cartService.doListByIds(ids);
        List<MemberAddress> adds = memberAddressService.doListById(OrderSessionUtil.getCurrent().getId());
        // 转发选中的商品信息
        model.addAttribute("carts", list);
        model.addAttribute("ids", ids);

        // 转发个人的地址
        model.addAttribute("adds", adds);

        return URLUtil.CONFIRM_HTML;
    }



}
